﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	MetaWebContentType.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Diagnostics;
using Microsoft.SharePoint;

namespace Smart.Common
{
	/// <summary>
	/// SharePoint content type
	/// </summary>
	[DebuggerDisplay("MetaWebContentType Name={Name} ID={ID}")]
	public class MetaWebContentType : MetaFields
	{
		/// <summary>
		/// Content type id
		/// </summary>
		public string ID { get; set; }

		/// <summary>
		/// Parent content type id
		/// </summary>
		public string ParentID { get; set; }

		/// <summary>
		/// Container scope
		/// </summary>
		public MetaWebScope Scope { get; set; }

		/// <summary>
		/// Container list (null, if scope content type)
		/// </summary>
		public MetaWebList List { get; set; }

		/// <summary>
		/// DispForm url
		/// </summary>
		public string DispFormUrl { get; set; }

		/// <summary>
		/// NewForm url
		/// </summary>
		public string NewFormUrl { get; set; }

		/// <summary>
		/// EditForm url
		/// </summary>
		public string EditFormUrl { get; set; }

		/// <summary>
		/// SharePoint content type
		/// </summary>
		public MetaWebContentType(string id, string parentID, string name, MetaWebList list)
			: base(name)
		{
			ID = id;
			ParentID = parentID;
			List = list;
			Scope = (MetaWebScope)list.Scope;
		}

		/// <summary>
		/// SharePoint content type
		/// </summary>
		public MetaWebContentType(string id, string parentID, string name, MetaWebScope scope)
			: base(name)
		{
			ID = id;
			ParentID = parentID;
			List = null;
			Scope = scope;
		}

		public override void LoadFields(object source)
		{
			var contentType = (SPContentType)source;

			foreach (SPField field in contentType.Fields)
			{
				try
				{
					var mf = new MetaField(field.Id, field.InternalName, field.Title, field.Type, List);

					FieldIDs[mf.Name] = mf.ID;
					FieldIDs[mf.Title] = mf.ID;
					FieldIDs[field.StaticName] = mf.ID;
					FieldIDs[mf.ID.ToString("D")] = mf.ID;
					FieldIDs[mf.ID.ToString("B")] = mf.ID;

					Fields[mf.ID] = mf;
				}
				catch (Exception x)
				{
					H.LX("MetaWebContentType.LoadFields", "LoadFields field.Id: " + field.Id, x);
				}
			}
		}
	}
}